package com.ocom.srvpaccess.controller;

import com.ocom.common.constant.SrvpAccessDeviceUrl;
import com.ocom.common.entity.access.EntityMjDeviceInfo;
import com.ocom.common.entity.srvpaccess.XfDeviceInfo;
import com.ocom.common.enums.MachEnum;
import com.ocom.common.resp.RespMachMessage;
import com.ocom.common.utils.RespUtilMach;
import com.ocom.srvpaccess.service.CompanyKeyInfoService;
import com.ocom.srvpaccess.service.MjDeviceInfoService;
import com.ocom.srvpaccess.service.XFDeviceInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;


@Slf4j
@RestController
public class KeyController {
    @Autowired
    private MjDeviceInfoService mjDeviceInfoService;

    @Autowired
    private CompanyKeyInfoService companyKeyInfoService;

    @Autowired
    private XFDeviceInfoService xfDeviceInfoService;

    // 获取解码密钥（getDecodeKey）
    @GetMapping(value = SrvpAccessDeviceUrl.URLP_ACCESSDEVICE_getDecodeKey)
    public RespMachMessage getDecodeKey(Long companyId,@RequestParam(required = false,defaultValue = "") String machNo,@RequestParam(required = false,defaultValue = "0") Integer deviceType){
        if(machNo.equals("19051497"))log.info("获取解码密钥（getDecodeKey）companyId 【{}】 machNo 【{}】 deviceType 【{}】" ,companyId,machNo,deviceType);
        //判断参数
        Long jobId=0l;
        if (deviceType==1){
            if (companyId <=0 || machNo==null){
                return RespUtilMach.error(MachEnum.PARAMS_ERROR);
            }
            //1、获取门禁设备表mj_device_info中com_id、device_no匹配的记录：
            EntityMjDeviceInfo deviceInfo = mjDeviceInfoService.getDeviceInfo(companyId, machNo);
            if (deviceInfo == null){
                return RespUtilMach.error(MachEnum.AUTH_ERROR);
            }
            jobId=deviceInfo.getRsaState();
        }else if(deviceType==2 || deviceType==0) {
            XfDeviceInfo entityXfDeviceInfo=xfDeviceInfoService.getParaJobid(companyId,machNo);
            if (entityXfDeviceInfo == null){
                return RespUtilMach.error(MachEnum.AUTH_ERROR);
            }
            jobId= Long.valueOf(entityXfDeviceInfo.getRsaJobid());
        }

        Map<String, Object> secret = companyKeyInfoService.selectByPublicKey(companyId);


        Map<String, Object> map = new HashMap<>();
        map.put("jobId", jobId);
        map.put("code_mode",0);
        map.put("secret",secret);
        return RespUtilMach.success(map);
    }

    // 更改密钥状态（changeKeyState）
    @PostMapping(value = SrvpAccessDeviceUrl.URLP_ACCESSDEVICE_changeKeyState)
    public RespMachMessage changeKeyState(Long companyId, String machNo,String jobId,@RequestParam(required = false,defaultValue = "1") Integer deviceType){
        int updataFlag=0;
        if (deviceType==1){
            //判断参数
            if (companyId <=0 || machNo==null|| jobId == null){
                return RespUtilMach.error(MachEnum.PARAMS_ERROR);
            }
            //1、获取门禁设备表mj_device_info中com_id、device_no匹配的记录：
            EntityMjDeviceInfo deviceInfo = mjDeviceInfoService.getDeviceInfo(companyId, machNo);
            try {
                mjDeviceInfoService.updateRsaState(deviceInfo.getComId(),deviceInfo.getDeviceNo(),jobId);
            }catch (Exception exception){
                exception.printStackTrace();
            }
        }else {
            if (!xfDeviceInfoService.updateRsa(companyId,machNo,jobId)){
                updataFlag=1;
            }
        }


        Map<String, Object> map = new HashMap<>();
        //标识本次通讯
        map.put("updataFlag", updataFlag);
        return RespUtilMach.success(map);
    }
}
